package com.zyx.comment.service.mq;

import com.alibaba.fastjson2.JSON;

import com.zyx.comment.model.Comment;
import com.zyx.comment.model.News;
import com.zyx.comment.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(
        topic = "news-topic",
        consumerGroup = "news-consumer-group",
        selectorType = SelectorType.TAG,
        selectorExpression = "NEW",
        instanceName = "NewsMessageConsumer",
        consumeMode = ConsumeMode.CONCURRENTLY // 并发消费模式
)
public class NewsMessageConsumer implements RocketMQListener<String> {

    @Autowired
    private CommentService commentService;

    @Override
    public void onMessage(String message) {
        log.info("[PushConsumer] 收到普通消息: " + message);
        // 业务处理逻辑
        News news = JSON.parseObject( message, News.class);
        Comment comment = new Comment();
        comment.setNewsId(news.getId());
        comment.setUserId(1);
        comment.setContent("评论：" + news.getContent());
        commentService.save(comment);

    }
}
